package pe.edu.upc.e41a.javasoft.jsf;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.application.RooJsfApplicationBean;

@ManagedBean
@RequestScoped
@Configurable
@RooJsfApplicationBean
public class ApplicationBean {

    public String getColumnName(String column) {
        if (column == null || column.length() == 0) {
            return column;
        }
        final Pattern p = Pattern.compile("[A-Z][^A-Z]*");
        final Matcher m = p.matcher(Character.toUpperCase(column.charAt(0)) + column.substring(1));
        final StringBuilder builder = new StringBuilder();
        while (m.find()) {
            builder.append(m.group()).append(" ");
        }
        return builder.toString().trim();
    }

	private MenuModel menuModelAdmin;
	private MenuModel menuModelOther;

	@PostConstruct
    public void init() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        /*MENU PARA EL USUARIO COMUN*/
        menuModelOther = new DefaultMenuModel();
        Submenu submenuOther;
        MenuItem itemOther;
        
        
        /*MENU PARA EL USUARIO ADMINISTRADOR*/
        menuModelAdmin = new DefaultMenuModel();
        Submenu submenuAdmin;
        MenuItem itemAdmin;
        
        submenuAdmin = new Submenu();
        submenuAdmin.setId("usuarioSubmenu");
        submenuAdmin.setLabel("Usuario");
        itemAdmin = new MenuItem();
        itemAdmin.setId("createUsuarioMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "Registrar Usuario", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{usuarioBean.registrarUsuario}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-document");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        menuModelAdmin.addSubmenu(submenuAdmin);
        
        
        submenuAdmin = new Submenu();
        submenuAdmin.setId("avisoSubmenu");
        submenuAdmin.setLabel("Aviso");
        itemAdmin = new MenuItem();
        itemAdmin.setId("createAvisoMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{messages.label_create}", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{avisoBean.displayCreateDialog}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-document");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        itemAdmin = new MenuItem();
        itemAdmin.setId("listAvisoMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{messages.label_list}", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{avisoBean.displayList}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-folder-open");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        menuModelAdmin.addSubmenu(submenuAdmin);
        
        
        submenuAdmin = new Submenu();
        submenuAdmin.setId("categoriaSubmenu");
        submenuAdmin.setLabel("Categoría");
        itemAdmin = new MenuItem();
        itemAdmin.setId("listCategoriaMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "Registrar Categoría", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{categoriaBean.displayList}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-folder-open");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        menuModelAdmin.addSubmenu(submenuAdmin);
        
 


        
        submenuAdmin = new Submenu();
        submenuAdmin.setId("tipoDocumentoSubmenu");
        submenuAdmin.setLabel("TipoDocumento");
        itemAdmin = new MenuItem();
        itemAdmin.setId("createTipoDocumentoMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{messages.label_create}", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{tipoDocumentoBean.displayCreateDialog}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-document");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        itemAdmin = new MenuItem();
        itemAdmin.setId("listTipoDocumentoMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{messages.label_list}", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{tipoDocumentoBean.displayList}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-folder-open");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        menuModelAdmin.addSubmenu(submenuAdmin);
        
        
        submenuAdmin = new Submenu();
        submenuAdmin.setId("tipoPersonaSubmenu");
        submenuAdmin.setLabel("TipoPersona");
        itemAdmin = new MenuItem();
        itemAdmin.setId("createTipoPersonaMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{messages.label_create}", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{tipoPersonaBean.displayCreateDialog}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-document");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        itemAdmin = new MenuItem();
        itemAdmin.setId("listTipoPersonaMenuItem");
        itemAdmin.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{messages.label_list}", String.class));
        itemAdmin.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{tipoPersonaBean.displayList}", String.class, new Class[0]));
        itemAdmin.setIcon("ui-icon ui-icon-folder-open");
        itemAdmin.setAjax(false);
        itemAdmin.setAsync(false);
        itemAdmin.setUpdate(":dataForm:data");
        submenuAdmin.getChildren().add(itemAdmin);
        menuModelAdmin.addSubmenu(submenuAdmin);
        
        //MENU PARA EL USUARIO COMUN//
        submenuOther = new Submenu();
        submenuOther.setId("transaccionSubmenu");
        submenuOther.setLabel("TRANSACCIONES");
        itemOther = new MenuItem();
        itemOther.setId("misComprasMenuItem");
        itemOther.setValueExpression("value", expressionFactory.createValueExpression(elContext, "Mis compras", String.class));
        itemOther.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{misTransaccionesBean.displayListCompras}", String.class, new Class[0]));
        itemOther.setIcon("ui-icon ui-icon-document");
        itemOther.setAjax(false);
        itemOther.setAsync(false);
        itemOther.setUpdate(":dataForm:data");
        submenuOther.getChildren().add(itemOther);
        itemOther = new MenuItem();
        itemOther.setId("misVentasMenuItem");
        itemOther.setValueExpression("value", expressionFactory.createValueExpression(elContext, "Mis ventas", String.class));
        itemOther.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{misTransaccionesBean.displayListVentas}", String.class, new Class[0]));
        itemOther.setIcon("ui-icon ui-icon-folder-open");
        itemOther.setAjax(false);
        itemOther.setAsync(false);
        itemOther.setUpdate(":dataForm:data");
        submenuOther.getChildren().add(itemOther);  
        menuModelOther.addSubmenu(submenuOther);
        
        submenuOther = new Submenu();
        submenuOther.setId("publicacionSubmenu");
        submenuOther.setLabel("PUBLICACIÓN");
        itemOther = new MenuItem();
        itemOther.setId("crearPublicacionMenuItem");
        itemOther.setValueExpression("value", expressionFactory.createValueExpression(elContext, "Crear nueva Publicación", String.class));
        itemOther.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{publicacionBean.registrarPublicacion}", String.class, new Class[0]));
        itemOther.setIcon("ui-icon ui-icon-document");
        itemOther.setAjax(false);
        itemOther.setAsync(false);
        itemOther.setUpdate(":dataForm:data");
        submenuOther.getChildren().add(itemOther);
        itemOther = new MenuItem();
        itemOther.setId("buscarPublicacionsMenuItem");
        itemOther.setValueExpression("value", expressionFactory.createValueExpression(elContext, "Buscar Publicación", String.class));
        itemOther.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{publicacionBean.displayList}", String.class, new Class[0]));
        itemOther.setIcon("ui-icon ui-icon-folder-open");
        itemOther.setAjax(false);
        itemOther.setAsync(false);
        itemOther.setUpdate(":dataForm:data");
        submenuOther.getChildren().add(itemOther);
        
        itemOther = new MenuItem();
        itemOther.setId("misPublicacionesMenuItem");
        itemOther.setValueExpression("value", expressionFactory.createValueExpression(elContext, "Mis Publicaciones", String.class));
        itemOther.setActionExpression(expressionFactory.createMethodExpression(elContext, "#{misTransaccionesBean.displayListPublicaciones}", String.class, new Class[0]));
        itemOther.setIcon("ui-icon ui-icon-folder-open");
        itemOther.setAjax(false);
        itemOther.setAsync(false);
        itemOther.setUpdate(":dataForm:data");
        submenuOther.getChildren().add(itemOther);
        menuModelOther.addSubmenu(submenuOther);
        
    }

	public MenuModel getMenuModelAdmin() {
        return menuModelAdmin;
    }

	public MenuModel getMenuModelOther() {
        return menuModelOther;
    }
	public String getAppName() {
        return "Sistema de Compra y Ventas";
    }
}